from business.business_base import BusinessBase


class AfterUpgradeBusiness(BusinessBase):
    """
    升级后执行业务
    """

    def __init__(self, **kwargs):
        super(AfterUpgradeBusiness, self).__init__(**kwargs)
        self.db_b_name_after = 'db_testb_after'
        self.t_b_name_after = 't_testb_after'
        self.t_row_compress_after = 't_row_compress_after'
        self.slot_name = 'slot_after'
        self.t_logical_relic_after = 't_logical_relic_after'
        self.up_user_prefix = 'upgrade_'
        self.tbs_name_after = 'tps_index_after'
        self.t_index_after = 't_index_after'
        self.index_name_after = 'i_index_after'

    def after_run_compatibility_b(self):
        """
        升级后，执行B库相关的业务:建B库，B库下dml
        :return: 无
        """
        self.log.info('升级后-执行B库相关的业务')
        self.check_db_compatibility_b(self.db_b_name_after,
                                      self.t_b_name_after)

    def after_check_tpcc(self):
        """
        升级后，执行tpcc业务，并检查tpcc数据一致性
        :return:无
        """
        self.log.info('升级后-执行tpcc业务，并检查tpcc数据一致性')
        self.run_tpcc('run')
        self.check_tpcc_data_consistency()

    def after_run_row_compress(self):
        """
        升级后，执行行存压缩、分区表相关业务
        :return: 无
        """
        self.log.info('升级后-执行行存压缩、分区表相关业务')
        if int(self.up_db_v[0].replace('.', '')) >= 300:
            self.row_compress_dml()
        elif int(self.up_db_v[-1].replace('.', '')) >= 300:
            self.create_row_compress(tb_name=self.t_row_compress_after)
            self.row_compress_dml(tb_name=self.t_row_compress_after)

    def after_check_index(self):
        """
        升级后，创建/修改/删除索引及分区表全局索引和分区索引业务
        :return: 无
        """
        self.log.info('升级后-执行索引相关业务')
        self.index(self.tbs_name_after, self.t_index_after,
                   self.index_name_after)

    def after_check_logical_replication(self, pre_res):
        """
        升级后，对比升级前逻辑解码一致，且校验创建逻辑复制槽，逻辑解码功能正常
        :param pre_res: 升级前执行逻辑解码的结果
        :return: 无
        """
        self.log.info('升级后-对比升级前逻辑解码一致，且校验创建逻辑复制槽，逻辑解码功能正常')
        after_res = self.logical_replication(create=False)
        assert pre_res == after_res

        self.logical_replication(slot_name=self.slot_name,
                                 tb_name=self.t_logical_relic_after)

    def after_check_table_consistency(self):
        """
        检查创建的各种表主备一致性
        :return: 无
        """
        self.check_table_consistency()

    def after_upgrade_business_main(self, pre_res):
        """
        执行升级后业务，并检查是否有core文件
        :param pre_res: 前置pre_run_logical_replication逻辑解码的结果
        :return: 无
        """
        self.init_business_db_data(self.up_user_prefix, self.db_b_name_after)
        self.after_run_row_compress()
        self.after_check_table_consistency()
        self.after_run_compatibility_b()
        self.after_check_index()
        self.after_check_tpcc()
        self.after_check_logical_replication(pre_res=pre_res)
